package com.learng.springcloud.ctrl;

import com.google.common.collect.Lists;
import com.learng.springcloud.entity.Member;
import com.learng.springcloud.service.MemberFeignService;
import com.learng.springcloud.service.MemberRestTemplateService;
import com.learng.springcloud.vo.MemberVo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

/**
 * Created by zhouwei on 2020/7/22.
 */
@RestController
@RequestMapping("/order")
public class OrderCtrl {

    @Autowired
    MemberRestTemplateService memberRestTemplateService;

    @RequestMapping("/getAllMember")
    public List<Member> getAllMemberRest(){
        return memberRestTemplateService.getAll();
    }

    int count = 1;

    @Autowired
    MemberFeignService memberFeignService;

    @HystrixCommand(fallbackMethod = "testCircuitBreakerBodyFallback", commandProperties = {
            // 是否启用服务熔断
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
            // 滚动窗口内请求阈值
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            // 断路器打开时间
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "100000"),
            // 滚动窗口内错误比率
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "10"),
            // 滚动窗口持续时间
            @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "2000")
    })
    @RequestMapping("/member/testCircuitBreakerBody")
    public List<Member> testCircuitBreakerBody(){
        await();
        System.out.println((count++)+ ": 正常................");
        return memberFeignService.getAll();
    }

    public List<Member> testCircuitBreakerBodyFallback(){
        System.out.println((count++)+ ": 降级................");
        return Lists.newArrayList(
                Member.builder().id(999L).name("testCircuitBreakerBody fallback").age(9999).build()
        );
    }


//    @HystrixCommand(
//            fallbackMethod = "getAllFallback"
//    )
    @RequestMapping("/member/getAll")
    public List<Member> getAllMemberFeign(){
        //await();
        return memberFeignService.getAll();
    }

    @RequestMapping("/member/findByCondition")
    List<Member> findByCondition(MemberVo condition){
        //await();
        return memberFeignService.findByCondition(condition);
    }

    @RequestMapping("/member/findByRequestParam")
    List<Member> findByRequestParam(MemberVo condition){
        //await();
        return memberFeignService.findByRequestParam(condition.getId(), condition.getName());
    }

    @RequestMapping("/member/findByRequestBody")
    public List<Member> findByRequestBody(@RequestBody MemberVo condition){
        //await();
        return memberFeignService.findByRequestBody(condition);
    }


    //test
    public List<Member> getAllFallback(){
        return Lists.newArrayList(
                Member.builder().id(999L).name("fallback").age(9999).build()
        );
    }

    public void await(){
        int sleepMillis = new Random().nextInt(100);
        try {
            System.out.printf("await millis : %s", sleepMillis);
            System.out.println();
            Thread.sleep(sleepMillis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(sleepMillis > 50){
            throw new RuntimeException("11");
        }
    }

}
